home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GDEVMEM1.C < prev    next >
C/C++ Source or Header  |  1992-03-25  |  16KB  |  515 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevmem1.c */
  21. /* Generic and monobit "memory" (stored bitmap) device */
  22. /* for Ghostscript library. */
  23. #include "memory_.h"
  24. #include "gs.h"
  25. #include "gxdevice.h"
  26. #include "gxdevmem.h"            /* semi-public definitions */
  27. #include "gdevmem.h"            /* private definitions */
  28.  
  29. /* Define the chunk size for monobit operations. */
  30. #if arch_is_big_endian
  31. #  define mono_chunk uint
  32. #else
  33. #  define mono_chunk ushort
  34. #endif
  35.  
  36. /* ------ Generic code ------ */
  37.  
  38. /* Return the appropriate memory device for a given */
  39. /* number of bits per pixel (0 if none suitable). */
  40. gx_device_memory *
  41. gdev_mem_device_for_bits(int bits_per_pixel)
  42. {    switch ( bits_per_pixel )
  43.        {
  44.     case 1: return &mem_mono_device;
  45.     case 8: return &mem_mapped8_color_device;
  46.     case 16: return &mem_true16_color_device;
  47.     case 24: return &mem_true24_color_device;
  48.     case 32: return &mem_true32_color_device;
  49.     default: return 0;
  50.        }
  51. }
  52.  
  53. /* Compute the size of the bitmap storage, */
  54. /* including the space for the scan line pointer table. */
  55. /* Note that scan lines are padded to a multiple of 4 bytes. */
  56. ulong
  57. gdev_mem_bitmap_size(const gx_device_memory *dev)
  58. {    unsigned raster =
  59.         ((dev->width * dev->color_info.depth + 31) >> 5) << 2;
  60.     mdev->raster = raster;
  61.     return (ulong)dev->height * (raster + sizeof(byte *));
  62. }
  63.  
  64. /* 'Open' the memory device and create the scan line table. */
  65. int
  66. mem_open(gx_device *dev)
  67. {    byte *scan_line = mdev->base;
  68.     uint raster = mdev->raster;
  69.     byte **pptr = (byte **)(scan_line + (uint)dev->height * raster);
  70.     byte **pend = pptr + dev->height;
  71.     mdev->line_ptrs = pptr;
  72.     while ( pptr < pend )
  73.        {    *pptr++ = scan_line;
  74.         scan_line += raster;
  75.        }
  76.     mdev->bytes_le =
  77. #if arch_is_big_endian
  78.         0
  79. #else
  80.         /* NOTE: mem_mono_get_bits relies on the fact that */
  81.         /* sizeof(mono_chunk) == 2! */
  82.         (mdev->color_info.depth < 8 ? sizeof(mono_chunk) : 0);
  83. #endif
  84.         ;
  85.     return 0;
  86. }
  87.  
  88. /* Return the initial transformation matrix */
  89. void
  90. mem_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
  91. {    *pmat = mdev->initial_matrix;
  92. }
  93.  
  94. /* Test whether a device is a memory device */
  95. int
  96. gs_device_is_memory(const gx_device *dev)
  97. {    /* We can't just compare the procs, or even an individual proc, */
  98.     /* because we might be tracing.  Compare the device name, */
  99.     /* and hope for the best. */
  100.     char *name = dev->dname;
  101.     int i;
  102.     for ( i = 0; i < 6; i++ )
  103.       if ( name[i] != "image("[i] ) return 0;
  104.     return 1;
  105. }
  106.  
  107. /* Ensure that the data bytes are in big-endian order. */
  108. /* This is never called on big-endian platforms; */
  109. /* on little-endian platforms, the chunk size is ushort, */
  110. /* regardless of the size of an int. */
  111. void
  112. gdev_mem_ensure_byte_order(gx_device_memory *dev)
  113. {
  114. #if !arch_is_big_endian
  115.     if ( !dev->bytes_le ) return;    /* already in order */
  116.     memswab(dev->base, dev->base, dev->raster * dev->height);
  117.     dev->bytes_le = 0;
  118. #endif
  119. }
  120.  
  121. /* Copy one or more scan lines to a client. */
  122. #undef chunk
  123. #define chunk byte
  124. int
  125. mem_get_bits(gx_device *dev, int y, byte *str, uint size, int pad_to_word)
  126. {    uint bytes_per_line =
  127.         gx_device_bytes_per_scan_line(dev, pad_to_word);
  128.     byte *src = scan_line_base(mdev, y);
  129.     byte *dest = str;
  130.     uint count = min(size / bytes_per_line, dev->height - y);
  131.     int swap = mdev->bytes_le;
  132.     if ( !mdev->raster )        /* compute it now */
  133.         (void)gdev_mem_bitmap_size(mdev);
  134.     if ( bytes_per_line == mdev->raster )
  135.        {    if ( swap && pad_to_word >= 0 )
  136.             memswab(src, dest, bytes_per_line * count);
  137.         else
  138.             memcpy(dest, src, bytes_per_line * count);
  139.        }
  140.     else                /* know pad_to_word == 0 */
  141.        {    uint c;
  142.         for ( c = count; c-- != 0; )
  143.            {    if ( swap )
  144.                {    /* We have to take extra care if */
  145.                 /* bytes_per_line is odd. */
  146.                 if ( bytes_per_line & 1 )
  147.                    {    memswab(src, dest, bytes_per_line - 1);
  148.                     dest[bytes_per_line - 1] =
  149.                       src[bytes_per_line];
  150.                    }
  151.                 else
  152.                     memswab(src, dest, bytes_per_line);
  153.                }
  154.             else
  155.                 memcpy(dest, src, bytes_per_line);
  156.             src += mdev->raster;
  157.             dest += bytes_per_line;
  158.            }
  159.        }
  160.     return (swap && pad_to_word < 0 ? swap : 0);
  161. }
  162.  
  163. /* ------ Monochrome ------ */
  164.  
  165. /* Procedures */
  166. private dev_proc_copy_mono(mem_mono_copy_mono);
  167. private dev_proc_fill_rectangle(mem_mono_fill_rectangle);
  168.  
  169. /* The device descriptor. */
  170. private gx_device_procs mem_mono_procs =
  171.   mem_procs(gx_default_map_rgb_color, gx_default_map_color_rgb,
  172.     mem_mono_copy_mono, gx_default_copy_color, mem_mono_fill_rectangle);
  173.  
  174. /* The instance is public. */
  175. gx_device_memory mem_mono_device =
  176.   mem_device("image(mono)", 1, mem_mono_procs);
  177.  
  178. /* Convert x coordinate to byte offset in scan line. */
  179. #define x_to_byte(x) ((x) >> 3)
  180.  
  181. /* Fill a rectangle with a color. */
  182. #undef chunk
  183. #define chunk mono_chunk
  184. private int
  185. mem_mono_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  186.   gx_color_index color)
  187. {    uint bit;
  188.     chunk right_mask;
  189.     byte fill;
  190.     declare_scan_ptr(dest);
  191.     check_rect();
  192.     setup_rect(dest);
  193. #define write_loop(stat)\
  194.  { int line_count = h;\
  195.    chunk *ptr = dest;\
  196.    do { stat; inc_chunk_ptr(ptr, draster); }\
  197.    while ( --line_count );\
  198.  }
  199. #define write_partial(msk)\
  200.    if ( fill ) write_loop(*ptr |= msk)\
  201.    else write_loop(*ptr &= ~msk)
  202.     switch ( color )
  203.        {
  204.     case 0: fill = mdev->invert; break;
  205.     case 1: fill = ~mdev->invert; break;
  206.     case gx_no_color_index: return 0;        /* transparent */
  207.     default: return -1;        /* invalid */
  208.        }
  209.     bit = x & chunk_bit_mask;
  210.     if ( bit + w <= chunk_bits )
  211.        {    /* Only one word. */
  212.         right_mask =
  213.           (w == chunk_bits ? chunk_all_bits : chunk_hi_bits(w))
  214.             >> bit;
  215.        }
  216.     else
  217.        {    int byte_count;
  218.         if ( bit )
  219.            {    /* We have to split the following statement */
  220.             /* into two because of a bug in the DEC */
  221.             /* VAX/VMS C compiler. */
  222.             chunk mask = chunk_all_bits;
  223.             mask >>= bit;
  224.             write_partial(mask);
  225.             dest++;
  226.             w += bit - chunk_bits;
  227.            }
  228.         right_mask = chunk_hi_bits(w & chunk_bit_mask);
  229.         if ( (byte_count = (w >> 3) & -chunk_bytes) != 0 )
  230.            {    write_loop(memset(ptr, fill, byte_count));
  231.             inc_chunk_ptr(dest, byte_count);
  232.            }
  233.        }
  234.     if ( right_mask )
  235.         write_partial(right_mask);
  236.     return 0;
  237. }
  238.  
  239. /* Copy a monochrome bitmap. */
  240.  
  241. /* Fetch a chunk from the source. */
  242. /* Note that the source data are always stored big-endian. */
  243. /* Note also that the macros always cast cptr, */
  244. /* so it doesn't matter what the type of cptr is. */
  245. #undef chunk
  246. #if arch_is_big_endian
  247. #  define chunk uint
  248. #  define cfetch(cptr) (*(chunk *)(cptr))
  249. #else
  250. #  define chunk ushort
  251. #  define cfetch(cptr) (((chunk)*(byte *)(cptr) << 8) + ((byte *)(cptr))[1])
  252. #endif
  253. /* Fetch a chunk that straddles a chunk boundary. */
  254. /***
  255. #if arch_is_big_endian
  256. ***/
  257. #  define cfetch2(cptr, cskew, skew)\
  258.     ((cfetch(cptr) << cskew) + (cfetch((chunk *)(cptr) + 1) >> skew))
  259. /***
  260. #else
  261. #  define cfetch2(cptr, cskew, skew)\
  262.     (cskew <= 8 ?\
  263.      (cfetch(cptr) << cskew) + (((byte *)(cptr))[2] >> (skew - 8)) :\
  264.      (((byte *)(cptr))[1] << cskew) + (cfetch((chunk *)(cptr) + 1) >> skew))
  265. #endif
  266. ***/
  267.  
  268. /* copy_function and copy_shift get added together for dispatch */
  269. typedef enum {
  270.     copy_or = 0, copy_store, copy_and, copy_funny
  271. } copy_function;
  272. typedef enum {
  273.     copy_right = 0, copy_left = 4
  274. } copy_shift;
  275. typedef struct {
  276.     short invert;
  277.     ushort op;            /* copy_function */
  278. } copy_mode;
  279. /* Map from <c0,c1,invert> to copy_mode. */
  280. #define cm(i,op) { i, (ushort)op }
  281. private copy_mode copy_modes[9*2] = {
  282.     cm(-1, copy_funny),        /* NN */
  283.     cm(-1, copy_and),        /* N0 */
  284.     cm(0, copy_or),            /* N1 */
  285.     cm(0, copy_and),        /* 0N */
  286.     cm(0, copy_funny),        /* 00 */
  287.     cm(0, copy_store),        /* 01 */
  288.     cm(-1, copy_or),        /* 1N */
  289.     cm(-1, copy_store),        /* 10 */
  290.     cm(0, copy_funny),        /* 11 */
  291.     cm(-1, copy_funny),        /* NNi */
  292.     cm(0, copy_or),            /* N1i */
  293.     cm(-1, copy_and),        /* N0i */
  294.     cm(-1, copy_or),        /* 1Ni */
  295.     cm(0, copy_funny),        /* 11i */
  296.     cm(-1, copy_store),        /* 10i */
  297.     cm(0, copy_and),        /* 0Ni */
  298.     cm(0, copy_store),        /* 01i */
  299.     cm(0, copy_funny)        /* 00i */
  300. };
  301. private int
  302. mem_mono_copy_mono(gx_device *dev,
  303.   byte *base, int sourcex, int sraster, gx_bitmap_id id,
  304.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  305. {    register byte *bptr;        /* actually chunk * */
  306.     int dbit, wleft;
  307.     uint mask;
  308.     copy_mode mode;
  309. #define function (copy_function)(mode.op)
  310.     declare_scan_ptr_as(dbptr, byte *);
  311. #define optr ((chunk *)dbptr)
  312.     register int skew;
  313.     register uint invert;
  314.     check_rect();
  315. #if gx_no_color_value != -1        /* hokey! */
  316.     if ( zero == gx_no_color_index ) zero = -1;
  317.     if ( one == gx_no_color_index ) one = -1;
  318. #endif
  319. #define izero (int)zero
  320. #define ione (int)one
  321.     mode =
  322.       copy_modes[(mdev->invert & 9) + izero + izero + izero + ione + 4];
  323. #undef izero
  324. #undef ione
  325.     invert = (uint)(int)mode.invert;    /* load register */
  326.     setup_rect_as(dbptr, byte *);
  327.     bptr = base + ((sourcex & ~chunk_bit_mask) >> 3);
  328.     dbit = x & chunk_bit_mask;
  329.     skew = dbit - (sourcex & chunk_bit_mask);
  330.     mask = chunk_all_bits >> dbit;
  331. /* Macros for writing partial chunks. */
  332. /* The destination pointer is always named optr, */
  333. /* and must be declared as chunk *. */
  334. /* cinvert may be temporarily redefined. */
  335. #define cinvert(bits) ((bits) ^ invert)
  336. #define write_or_masked(bits, mask, off)\
  337.   optr[off] |= (cinvert(bits) & mask)
  338. #define write_store_masked(bits, mask, off)\
  339.   optr[off] = ((optr[off] & ~mask) | (cinvert(bits) & mask))
  340. #define write_and_masked(bits, mask, off)\
  341.   optr[off] &= (cinvert(bits) | ~mask)
  342. /* Macros for writing full chunks. */
  343. #define write_or(bits)  *optr |= cinvert(bits)
  344. #define write_store(bits) *optr = cinvert(bits)
  345. #define write_and(bits) *optr &= cinvert(bits)
  346. /* Macro for incrementing to next chunk. */
  347. #define next_x_chunk\
  348.   bptr += chunk_bytes; dbptr += chunk_bytes
  349. /* Common macro for the end of each scan line. */
  350. #define end_y_loop(sdelta, ddelta)\
  351.   if ( --h == 0 ) break;\
  352.   bptr += sdelta; dbptr += ddelta
  353.     if ( (wleft = w + dbit - chunk_bits) <= 0 )
  354.        {    /* The entire operation fits in one (destination) chunk. */
  355.         /* Some machines can't handle w == chunk_bits! */
  356. #if arch_cant_shift_full_chunk
  357.         if ( w != chunk_bits )
  358. #endif
  359.           mask -= mask >> w;
  360. #define write_single(wr_op, src)\
  361.   for ( ; ; )\
  362.    { wr_op(src, mask, 0);\
  363.      end_y_loop(sraster, draster);\
  364.    }
  365. #define write1_loop(src)\
  366.   switch ( function ) {\
  367.     case copy_or: write_single(write_or_masked, src); break;\
  368.     case copy_store: write_single(write_store_masked, src); break;\
  369.     case copy_and: write_single(write_and_masked, src); break;\
  370.     default: goto funny;\
  371.   }
  372.         if ( skew >= 0 )    /* single -> single, right/no shift */
  373.            {    write1_loop(cfetch(bptr) >> skew);
  374.            }
  375.         else if ( wleft <= skew )    /* single -> single, left shift */
  376.            {    skew = -skew;
  377.             write1_loop(cfetch(bptr) << skew);
  378.            }
  379.         else            /* double -> single */
  380.            {    int cskew = -skew;
  381.             skew += chunk_bits;
  382.             write1_loop(cfetch2(bptr, cskew, skew));
  383.            }
  384. #undef write1_loop
  385. #undef write_single
  386.        }
  387.     else if ( wleft <= skew )
  388.        {    /* 1 source chunk -> 2 destination chunks. */
  389.         /* This is an important special case for */
  390.         /* both characters and halftone tiles. */
  391.         register uint bits;
  392.         uint rmask = chunk_hi_bits(wleft);
  393.         int cskew = chunk_bits - skew;
  394. #define write_1to2(wr_op)\
  395.   for ( ; ; )\
  396.    { bits = cfetch(bptr) ^ invert;\
  397.      wr_op(bits >> skew, mask, 0);\
  398.      wr_op(bits << cskew, rmask, 1);\
  399.      end_y_loop(sraster, draster);\
  400.    }
  401. #undef cinvert
  402. #define cinvert(bits) (bits)        /* pre-inverted here */
  403.         switch ( function )
  404.            {
  405.         case copy_or: write_1to2(write_or_masked); break;
  406.         case copy_store: write_1to2(write_store_masked); break;
  407.         case copy_and: write_1to2(write_and_masked); break;
  408.         default: goto funny;
  409.            }
  410. #undef cinvert
  411. #define cinvert(bits) ((bits) ^ invert)
  412. #undef write_1to2
  413.        }
  414.     else
  415.        {    /* More than one source chunk and more than one */
  416.         /* destination chunk are involved. */
  417.         uint rmask = chunk_hi_bits(wleft & chunk_bit_mask);
  418.         int words = (wleft & ~chunk_bit_mask) >> 3;
  419.         uint sskip = sraster - words;
  420.         uint dskip = draster - words;
  421.         register uint bits;
  422.         if ( skew == 0 )    /* optimize the aligned case */
  423.            {
  424. #define write_aligned(wr_op, wr_op_masked)\
  425.   for ( ; ; )\
  426.    { int count = wleft;\
  427.      /* Do first partial chunk. */\
  428.      wr_op_masked(cfetch(bptr), mask, 0);\
  429.      /* Do full chunks. */\
  430.      while ( (count -= chunk_bits) >= 0 )\
  431.       { next_x_chunk; wr_op(cfetch(bptr)); }\
  432.      /* Do last chunk */\
  433.      if ( count > -chunk_bits )\
  434.       { wr_op_masked(cfetch(bptr + chunk_bytes), rmask, 1); }\
  435.      end_y_loop(sskip, dskip);\
  436.    }
  437.             switch ( function )
  438.               {
  439.               case copy_or:
  440.                 write_aligned(write_or, write_or_masked);
  441.                 break;
  442.               case copy_store:
  443.                 write_aligned(write_store, write_store_masked);
  444.                 break;
  445.               case copy_and:
  446.                 write_aligned(write_and, write_and_masked);
  447.                 break;
  448.               default:
  449.                 goto funny;
  450.               }
  451. #undef write_aligned
  452.            }
  453.         else            /* not aligned */
  454.            {    int ccase =
  455.               (skew >= 0 ? copy_right :
  456.                ((bptr += chunk_bytes), copy_left)) + function;
  457.             int cskew = -skew & chunk_bit_mask;
  458.             skew &= chunk_bit_mask;
  459.             for ( ; ; )
  460.                {    int count = wleft;
  461. #define prefetch_right\
  462.   bits = cfetch(bptr) >> skew
  463. #define prefetch_left\
  464.   bits = cfetch2(bptr - chunk_bytes, cskew, skew)
  465. #define write_unaligned(wr_op, wr_op_masked)\
  466.   wr_op_masked(bits, mask, 0);\
  467.   /* Do full chunks. */\
  468.   while ( count >= chunk_bits )\
  469.     { bits = cfetch2(bptr, cskew, skew);\
  470.       next_x_chunk; wr_op(bits); count -= chunk_bits;\
  471.     }\
  472.   /* Do last chunk */\
  473.   if ( count > 0 )\
  474.     { bits = cfetch(bptr) << cskew;\
  475.       if ( count > skew ) bits += cfetch(bptr + chunk_bytes) >> skew;\
  476.       wr_op_masked(bits, rmask, 1);\
  477.     }
  478.                 switch ( ccase )
  479.                   {
  480.                   case copy_or + copy_left:
  481.                     prefetch_left; goto uor;
  482.                   case copy_or + copy_right:
  483.                     prefetch_right;
  484. uor:                    write_unaligned(write_or, write_or_masked);
  485.                     break;
  486.                   case copy_store + copy_left:
  487.                     prefetch_left; goto ustore;
  488.                   case copy_store + copy_right:
  489.                     prefetch_right;
  490. ustore:                    write_unaligned(write_store, write_store_masked);
  491.                     break;
  492.                   case copy_and + copy_left:
  493.                     prefetch_left; goto uand;
  494.                   case copy_and + copy_right:
  495.                     prefetch_right;
  496. uand:                    write_unaligned(write_and, write_and_masked);
  497.                     break;
  498.                   default:
  499.                     goto funny;
  500.                   }
  501.                 end_y_loop(sskip, dskip);
  502. #undef write_unaligned
  503. #undef prefetch_left
  504. #undef prefetch_right
  505.                }
  506.            }
  507.        }
  508. #undef end_y_loop
  509. #undef next_x_chunk
  510.     return 0;
  511.     /* Handle the funny cases that aren't supposed to happen. */
  512. funny:    return (invert ? -1 : mem_mono_fill_rectangle(dev, x, y, w, h, zero));
  513. #undef optr
  514. }
  515.